// pages/index/free/final_result.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    clickId: -1,
    city: '',
    acerage: '',
    houseroom: '',
    decorage: '',
    sum: '',
    matercial: '',
    rengong: '',
    sid: '',
    bid_id: '',
    uuid: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    let dataArr = options.data.split(',');
    var city = dataArr[0];
    var acage = dataArr[1];
    var sum = dataArr[2];
    var decorage = dataArr[6];
    var labour = dataArr[3];
    var matercial = dataArr[4];
    var rengong = dataArr[5];
    var bid_id = dataArr[7] 
    console.log('初次加载', bid_id)
    var uuid = dataArr[8]
    console.log(uuid)
    console.log(dataArr)
    sid: ''
    console.log(dataArr)
    wx.getStorage({ //获取本地缓存
      key: "sid",
      success: function(res) {
        that.setData({
          sid: res.data
        });
      },
    })


    this.setData({
      city: city,
      acerage: acage,
      sum: sum,
      decorage: decorage,
      labour: labour,
      matercial: matercial,
      rengong: rengong,
      bid_id: bid_id,
      uuid: uuid
    })
    setTimeout(function() {
      that.matercial()
    }, 100)

  },
  matercial: function(e) {
    var acreage = this.data.acerage
    var that = this
    var sid = that.data.sid
    console.log(sid)
    wx.request({
      url: app.apiUrl('c=zhaobiao/index&a=get_material_unitprice'),
      data: {
        acreage: acreage
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': 'PHPSESSID=' + sid
      },
      method: "GET",
      success: function(res) {
        console.log('请求成功', res.data)
        var polist = res.data.datas
        that.setData({
          thepolist: polist
        })
      },
      fail: function(res) {
        console.log('请求失败', res.data)
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  applon: function(e) {
    var that = this;
    var length = that.data.clickId;
    var index = parseInt(e.currentTarget.id);
    var types = e.currentTarget.dataset.type
    var bid_id = that.data.bid_id
    console.log('bin_id是', bid_id)
    var uuid = that.data.uuid
    var sid = that.data.sid
    if (this.data.clickId == e.currentTarget.id) {
      this.setData({
        clickId: 1,
      })
      return;
    }
    that.setData({
      clickId: e.currentTarget.id,
      showmask: true,
      showdialog: true,
    })
    wx.request({
      url: app.apiUrl("c=zhaobiao/index&a=update_material_info"),
      data: {
        material_type: types,
        bid_id: bid_id,
        uuid: uuid
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': 'PHPSESSID=' + sid
      },
      method: 'POST',
      success: function(res) {
        console.log(res.data)
        var code = res.data.code
        //var msg =res.data.datas.msg
        if (code == 200) {
          that.setData({
            showmask: true,
            showdialog: true,
            open: true,
            order: '预约成功！'
          }) 
          setTimeout(function() {
            that.setData({
              showmask: false,
              showdialog: false,
              open: false,
            })
          }, 2000)
          setTimeout(function(){
            wx.switchTab({
              url: '../index' //到首页
            })
          },5000)
         
        } else if (code == 400) {
            that.setData({
              showmask: true,
              showdialog: true,
              open: false,
              order: '你已预约！'
            })
          setTimeout(function () {
            that.setData({
              showmask: false,
              showdialog: false,
              open: false
            })
          
          }, 2000)
          setTimeout(function () {
            wx.switchTab({
              url: '../index' //到首页
            })
          },3000)
        }
      }
    })
    console.log(e)
  },


})